package com.top.horizon.th1804app.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.net.protocol.model.RecordFile;
import com.top.horizon.th1804app.R;
import com.top.horizon.th1804app.bean.PlayListBean;
import com.top.horizon.th1804app.utils.ToastUitl;

import java.util.ArrayList;
import java.util.List;

public class PlaylistAdapter extends RecyclerView.Adapter<PlaylistAdapter.PlaylistViewHolder> {
    private List<RecordFile> mList;
    private Context mContext;
    private PlaylistViewHolder playlistViewHolder;

    public PlaylistAdapter(List<RecordFile> list, Context context) {
        this.mContext = context;
        this.mList = list;
    }

    @Override
    public PlaylistViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View inflate = View.inflate(mContext, R.layout.play_list_item, null);
        playlistViewHolder = new PlaylistViewHolder(inflate);
        return playlistViewHolder;
    }

    @Override
    public void onBindViewHolder(PlaylistViewHolder holder, final int position) {
        final RecordFile playListBean = mList.get(position);
        holder.tvFileName.setText(playListBean.name);
        holder.tvFileSize.setText(String.valueOf(playListBean.size));
        holder.tvFileStart.setText("始：" + playListBean.startTime);
        holder.tvFileOver.setText("末：" + playListBean.endTime);
        holder.cbChecked.setChecked(playListBean.isSelected);
        holder.cbChecked.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    playListBean.isSelected = true;
                } else {
                    playListBean.isSelected = false;
                }
            }
        });
        holder.btnDetails.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPlayListAdapterListener.onClickChildView(playListBean);

            }
        });
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    private PlayListAdapterListener mPlayListAdapterListener;

    public void setPlayListAdapterListener(PlayListAdapterListener playListAdapterListener) {
        this.mPlayListAdapterListener = playListAdapterListener;
    }

    public interface PlayListAdapterListener {
        void checkedPosition(ArrayList<Integer> checkedList);

        void onClickChildView(RecordFile file);
    }


    public class PlaylistViewHolder extends RecyclerView.ViewHolder {
        TextView tvFileName, tvFileSize, tvFileStart, tvFileOver;
        public CheckBox cbChecked;
        TextView btnDetails;

        public PlaylistViewHolder(View itemView) {
            super(itemView);
            cbChecked = itemView.findViewById(R.id.cb_checked);
            tvFileName = itemView.findViewById(R.id.tv_file_name);
            tvFileSize = itemView.findViewById(R.id.tv_file_size);
            tvFileStart = itemView.findViewById(R.id.tv_file_start);
            tvFileOver = itemView.findViewById(R.id.tv_file_over);
            btnDetails = itemView.findViewById(R.id.btn_details);

        }
    }
}
